package com.czwj.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.CapitalApply;
import com.czwj.entities.DataGridData;
import com.czwj.entities.Form;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.ProjectItem;
import com.czwj.entities.UserInfo;
import com.czwj.service.CapitalApplyService;
import com.czwj.service.FormService;
import com.czwj.service.ProjectItemService;
import com.czwj.util.TextUtil;
import com.czwj.util.WechatUtil;

import net.sf.json.JSONObject;

@Controller
public class CapitalApplyAction {
	@Resource
	CapitalApplyService capitalApplyService;
	@Resource
	ProjectItemService projectItemService;
	@Resource
	FormService formService;
	
	@Value("${review_model_code}")
	private String model_code;
	
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");

	@RequestMapping(value = "/capitalApply.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<CapitalApply> capitalApply(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "projectitemid", required = false) String projectitemid,
			@RequestParam(value = "projectitemchildid", required = false) String projectitemchildid) {
		DataGridData<CapitalApply> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(status)){
			map.put("status", status);
		}
		if(TextUtil.isNotEmpty(projectitemid)&&!projectitemid.equals("0")){
			map.put("projectitemid", projectitemid);
		}
		if(TextUtil.isNotEmpty(projectitemchildid)&&!projectitemchildid.equals("0")){
			map.put("projectitemchildid", projectitemchildid);
		}
		List<CapitalApply> list = capitalApplyService.searchbylevel3(map);
		result.setRows(list);
		result.setTotal(capitalApplyService.countbylevel3(map));
		return result;
	}
	
	@SystemControllerLog(description = "修改资金申请") 
	@RequestMapping(value = "/capitalApplySave.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult capitalApplySave(@RequestBody CapitalApply capitalApply, HttpServletRequest request)
			throws Exception {
		OperateResult result = new OperateResult();
		int count = 0;
		if (capitalApply.getId() != null && capitalApply.getId() > 0) {
			count = capitalApplyService.update(capitalApply);
		} else {
			Subject currentUser = SecurityUtils.getSubject();
			UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
			capitalApply.setCreaterid(userInfo.getId());
			capitalApply.setCreatedate(new Date());
			capitalApply.setCreateip(request.getRemoteAddr());
			capitalApply.setStatus(6);
			count = capitalApplyService.insert(capitalApply);
		}
		ProjectItem projectItem = projectItemService.selectByPrimaryKey(capitalApply.getProjectitemid());
		for (UserInfo user : projectItem.getLevel2()) {
			JSONObject data = WechatUtil.setReviewData(projectItem.getName()+" 资金申请", sdf2.format(new Date()));
			sendReview(user, data);
		}
		result.setSuccess(count > 0);
		return result;
	}

	@SystemControllerLog(description = "禁用资金申请")
	@RequestMapping(value = "capitalApplyDelete.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult deleteCapitalApply(@RequestParam("id") int id) {
		OperateResult result = new OperateResult();
		try {
			int count = capitalApplyService.updateStatus(id);
			result.setSuccess(count > 0);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setSuccess(false);
			return result;
		}
	}
	
	@SystemControllerLog(description = "删除资金申请")
	@RequestMapping(value = "capitalApplyRemove.do", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult removeCapitalApply(@RequestParam("id") int id) {
		OperateResult result = new OperateResult();
		try {
			int count = capitalApplyService.remove(id);
			result.setSuccess(count > 0);
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setSuccess(false);
			return result;
		}
	}

	@RequestMapping(value = "/review/capitalapply.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<CapitalApply> unreviewedCapitalApply(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "projectitemid", required = false) String projectitemid) {
		DataGridData<CapitalApply> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid", userInfo.getId());
		if(TextUtil.isNotEmpty(projectitemid)&&!projectitemid.equals("0")){
			map.put("projectitemid", projectitemid);
		}
		int roleid = userInfo.getRoleid();
		if(roleid ==1 ||roleid ==2){
			map.put("status", 7);
			result.setRows(capitalApplyService.searchbylevel1(map));
			result.setTotal(capitalApplyService.countbylevel1(map));
		}
		if(roleid==3){
			map.put("status", 6);
			result.setRows(capitalApplyService.searchbylevel2(map));
			result.setTotal(capitalApplyService.countbylevel2(map));
		}
		return result;
	}
	
	@RequestMapping(value = "/reviewed/capitalapply.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<CapitalApply> reviewedCapitalApply(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "projectitemid", required = false) String projectitemid) {
		DataGridData<CapitalApply> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(projectitemid)&&!projectitemid.equals("0")){
			map.put("projectitemid", projectitemid);
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid", userInfo.getId());
		int roleid = userInfo.getRoleid();
		if(roleid ==1 ||roleid ==2){
			map.put("status", 4);
			result.setRows(capitalApplyService.searchbylevel1(map));
			result.setTotal(capitalApplyService.countbylevel1(map));
		}
		if(roleid==3){
			map.put("status", "4,7");
			result.setRows(capitalApplyService.searchbylevel2(map));
			result.setTotal(capitalApplyService.countbylevel2(map));
		}
		return result;
	}
	
	@RequestMapping(value = "/capitalApplyReviewed.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<CapitalApply> capitalApplyReviewed(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows) {
		DataGridData<CapitalApply> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		map.put("userid", userInfo.getId());
		if (userInfo.getRoleid()==1||userInfo.getRoleid()==2) {
			map.put("status", "3,4");
			result.setRows(capitalApplyService.searchReviewedLevel1(map));
			result.setTotal(capitalApplyService.countReviewedLevel1(map));
		}else if (userInfo.getRoleid()==3) {
			map.put("status", "3,4,7");
			result.setRows(capitalApplyService.searchReviewedLevel2(map));
			result.setTotal(capitalApplyService.countReviewedLevel2(map));
		}
		return result;
	}
	
	@RequestMapping(value = "/getcapital.do", method = RequestMethod.GET)
	@ResponseBody
	public List<CapitalApply> getcapital(@RequestParam(value = "projectitemid", required = true) int projectitemid,
			@RequestParam(value = "itemid", required = true) int itemid,
			@RequestParam(value = "status", required = true) String status) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("projectitemid", projectitemid);
		map.put("itemid", itemid);
		map.put("status", status);
		List<CapitalApply> list = capitalApplyService.getcapital(map);
		return list;
	}
	
	private void sendReview(UserInfo userInfo,JSONObject data){
		List<Form> forms = formService.findbyuser(userInfo.getId());
		for (Form form : forms) {
			String code = WechatUtil.pushMessage(userInfo.getOpenid(), model_code, form.getFormid(),data);
			formService.deleteByPrimaryKey(form.getId());
			if(code.trim().equals("0")){
				return;
			}
		}
	}
}
